﻿using System;
using System.Collections.Generic;
using System.Text;

using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity.Context;
using System.IO;
using System.Text;
namespace LearnNet.Common
{
   public class NVelocityHelper 
    {
        private  VelocityEngine vltEngine;
        private IContext context = new VelocityContext();
        public bool IsFile = true;
        public NVelocityHelper(string loadPath)
        {

            vltEngine = new VelocityEngine();
            if (IsFile)
            {
                //初始化该实例对象
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); //加载类型
                
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, loadPath);//设置路径
                vltEngine.SetProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
                vltEngine.SetProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
                vltEngine.SetProperty("directive.set.null.allowed", true);   //可以为空
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);  //开启缓存
                vltEngine.SetProperty("file.resource.loader.modificationCheckInterval", (Int64)360000);
                vltEngine.SetProperty("resource.manager.class", "NVelocity.Runtime.Resource.ResourceManagerImpl");
                vltEngine.SetProperty("resource.manager.cache.class", "NVelocity.Runtime.Resource.ResourceCacheImpl");
                vltEngine.SetProperty("directive.foreach.maxloops", 4);      
            }
            vltEngine.Init();
        }

       /// <summary>
       /// 设置模板路径
       /// </summary>
       /// <param name="strPath"></param>
        public void SetLoaderPath(string strPath)
        {
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, strPath);
        }

        public void SetLoaderCache(bool isSet,Int32 times)
        {
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, isSet);  //开启缓存
            vltEngine.SetProperty("file.resource.loader.modificationCheckInterval", (Int64)times);
        }
       /// <summary>
       /// 是否重模板中生成
       /// </summary>
       /// <param name="isFile"></param>
        public NVelocityHelper(string loadPath,bool isFile)
        {
            vltEngine = new VelocityEngine();
            IsFile = isFile;
            if (IsFile)
            {
                //初始化该实例对象
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); //加载类型
               
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, loadPath);//设置路径
                vltEngine.SetProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
                vltEngine.SetProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
                vltEngine.SetProperty("directive.set.null.allowed", true);   //可以为空
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);  //开启缓存
                vltEngine.SetProperty("file.resource.loader.modificationCheckInterval", (Int64)360000);
                vltEngine.SetProperty("resource.manager.class", "NVelocity.Runtime.Resource.ResourceManagerImpl");
                vltEngine.SetProperty("resource.manager.cache.class", "NVelocity.Runtime.Resource.ResourceCacheImpl");
                vltEngine.SetProperty("directive.foreach.maxloops", 4);  
            }
            vltEngine.Init();
        }

        /// <summary>
        /// 给模板变量赋值
        /// </summary>
        /// <param name="key">
        /// 变量名称
        /// </param>
        public void Put(string key, object value)
        {
            if (context == null)
                context = new VelocityContext();
            context.Put(key, value);
        }
       /// <summary>
       /// 清除模板变量
       /// </summary>
       /// <param name="key"></param>
        public void Remove(string key)
        {
            if (context != null)
            {
                context.Remove(key);
            }
        }

       /// <summary>
       /// 全部清除模板变量
       /// </summary>
        public void RemoveAll()
        {
            if (context != null)
            {
                foreach (object obj in context.Keys)
                {
                    context.Remove(obj);
                }
            }
        }
        
        /// <summary>
        ///  显示模板
        /// </summary>
        /// <param name="templateFileName">模板地址</param>
        public  StringBuilder Display(string templateFileName)
        {
            StringWriter writer = new StringWriter();
            if (IsFile)
            {
                //从文件中读取模板
                var template = vltEngine.GetTemplate(templateFileName);

                //合并模板

                template.Merge(context, writer);
               
            }
            return writer.GetStringBuilder();
        }

        public string DisplayFromString(string strTemplate)
        {
            try
            {
                StringWriter writer = new StringWriter();
                vltEngine.Evaluate(context, writer, null, strTemplate);
                return writer.GetStringBuilder().ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
 
}
